package nl.alwinzwets.controllers
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class SoundEffect
	{
		
		private var _url:String;
		private var _urlReq:URLRequest;
		private var _locked:Boolean;
		public var sound:Sound;
		public var channel:SoundChannel;
		
		public function SoundEffect(url:String)
		{
			_url = url;
			_locked = false;
			loadSounds();
		}
		
		private function loadSounds():void
		{
			_urlReq = new URLRequest(_url);
			sound = new Sound();			
			channel = new SoundChannel();
			sound.load(_urlReq);
			sound.addEventListener(Event.COMPLETE, dispatchComplete);
		}
		
		private function dispatchComplete(e:Event):void
		{
			trace("soundloaded");			
		}
		
		public function play():void
		{
			if(channel.position == 0 || (channel.position/sound.length) > 0.8){
				channel = sound.play();
				_locked = true;	
			}
		}
		
		public function stop():void
		{
			
			channel.stop();
			
		}

	}
}